import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'timesGap'
})
export class TimesGapPipe implements PipeTransform {

    transform(value: any, args?: any): any {
        let today:any = new Date();
        let _value = (new Date(parseInt(value)));
        let yearGap = today.getFullYear() - _value.getFullYear() ;
        let MonthGap = today.getMonth() - _value.getMonth();
        let dayGap =  today.getDate() - _value.getDate();
        let hourGap = today.getHours() - _value.getHours();
        let minutesGap =  today.getMinutes() - _value.getMinutes();
        if (value && value < today.getTime()) {
            let result = '';
            if(yearGap >= 1) {
                result +=yearGap + '年';
            }
            if(MonthGap >= 1) {
                result += MonthGap + '个月';
            }
            if(dayGap >= 1) {
                result += dayGap + '天';
            }
            if(hourGap >= 1) {
                result += hourGap + '小时';
            }
            if(minutesGap >= 1) {
                result += minutesGap + '分钟';
            }
            return result;
        } else {
            return null;
        }
    }
}
